package com.luoqiu.yygh.msm.controller;

import com.luoqiu.yygh.common.result.Result;
import com.luoqiu.yygh.msm.service.MsmApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/msm")
public class MsmApiController {

    @Autowired
    private MsmApiService msmApiService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @GetMapping(value = "/send/{phone}")
    public Result<Object> send(@PathVariable String phone) {
        String code = redisTemplate.opsForValue().get(phone);
        if (code != null) {
            return Result.ok();
        }
        code = "0909";
        boolean success = msmApiService.send(phone, code);
        if (success) {
            redisTemplate.opsForValue().set(phone, code, 2, TimeUnit.SECONDS);
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

}
